﻿using DataService.Cache.Caching;
using DataService.Cache.IService;
using DataService.Cache.RedisService;
using DataService.Core.ServiceDTO;
using DataService.Core.ServiceDTO.Const;
using System;
using System.Collections.Generic;
using System.Text;

namespace UnitTest
{
    public class TestLock
    {
        private static readonly object _lock = new object();
        
        public void TestDBConntionLock(int k)
        {
            bool acquiredLock = false;
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId},时间：{DateTime.Now}，开始进入{k}");
            try
            {
                Monitor.TryEnter(_lock, 3000, ref acquiredLock);
                //Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId},时间：{DateTime.Now}，获取锁{k},锁标志位：{acquiredLock}");
                if (acquiredLock)
                {
                    throw new Exception("测试抛出异sssssss常后是否走到finally");

                }
                if (!acquiredLock)
                {
                    Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId},时间：{DateTime.Now}，未获取到锁{k}");
                    throw new Exception("测试抛出异常后是否走到finally");
                }

            }
            catch (Exception ex)
            { }
            finally
            {
                if (acquiredLock)
                {
                    Monitor.Exit(_lock);
                    Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId},时间：{DateTime.Now}，释放锁");
                }
            }

            Console.WriteLine($"完成{Thread.CurrentThread.ManagedThreadId}");
        }
        public void TestCacheLock_1()
        {
            ICache cache = Cache.DefaultInstance;
            using (var acquire=cache.AcquireLock("测试",1000))
            {
                Thread.Sleep(1000);
            }
            cache.Get<String>("测试");
        }
    }
}
